#include "HttpServer.hpp"
#include <memory>

#include <iostream>

using namespace std;
using namespace Server;

void Usage(const std::string &proc)
{
    std::cout << "\nUsage: \n\t" << proc << " httpPort\n"
              << std::endl;
}
// .html .jpg
std::string suffixToDesc(const std::string &suffix)
{
    std::string ct = "Content-Type: ";
    if (suffix == ".html")
        ct += "text/html";
    else if (suffix == ".jpg")
        ct += "application/x-jpg";
    ct += "\r\n";
    return ct;
}

bool Get(const HttpRequest &req, HttpResponse &resp)
{
    // test
    std::cout << "---------------- Http Start -----------------" << std::endl;
    std::cout << req.inbuffer << std::endl;
    std::cout << "method: " << req.method << std::endl;
    std::cout << "url: " << req.url << std::endl;
    std::cout << "httpversion: " << req.httpversion << std::endl;
    std::cout << "path: " << req.path << std::endl;
    std::cout << "suffix: " << req.suffix << std::endl;
    std::cout << "size: " << req.size << "字节" << std::endl;
    std::cout << "---------------- Http End -----------------" << std::endl;

    std::string respline = "HTTP/1.1 200 OK\r\n";
    // std::string respheader = "Content-Type: text/html\r\n";
    std::string respheader = suffixToDesc(req.suffix);

    // respheader += "Location: https://www.qq.com/\r\n";
    std::string respblank = "\r\n";
    // std::string body = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>for test</title><h1>hello world</h1></head><body><p>早熟的人都晚熟，骄傲的人又很急性。我知道一个人，就他每做一件小事的时候，他都像救命稻草一样抓着，有一天，我一看，好家伙，他抱着的是一颗已经让我仰望的参天大树了!</p></body></html>";
    std::string body;
    if (req.size < 0)
    {
        respline = "HTTP/1.1 404 Not Found\r\n";
        struct stat st;
        if (stat(html_404.c_str(), &st) == 0)
        {
            int real_size = st.st_size;
            body.resize(real_size);
            Util::readFile(html_404, (char *)body.c_str(), real_size);
            respheader += "Content-Length: " + std::to_string(real_size) + "\r\n";
        }
    }
    else
    {
        body.resize(req.size);
        Util::readFile(req.path, (char *)body.c_str(), req.size);
        respheader += "Content-Length: " + std::to_string(req.size) + "\r\n";
    }
    respheader += "Set-Cookie: name=1234567abcdefg; Max-Age=120\r\n";
    // 往后，每次http请求，都会携带自己曾经设置的cookie，帮服务器进行鉴权行为---http会话保持
    resp.outbuffer += respline;
    resp.outbuffer += respheader;

    resp.outbuffer += respblank;

    std::cout << "----------------------http response start---------------------------" << std::endl;
    std::cout << resp.outbuffer << std::endl;
    cout << "----------------------http response end---------------------------" << std::endl;

    resp.outbuffer += body;

    return true;
}

// ./HttpServer 端口号
int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }

    uint16_t httpPort = atoi(argv[1]);
    unique_ptr<HttpServer> httpsvr(new HttpServer(Get, httpPort));

    httpsvr->initServer();
    httpsvr->start();
    return 0;
}
